package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;

import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<Object> list = simpleExecutor
                .query(configuration, mappedStatement, params);
        return (List<E>) list;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> objects = selectList(statementId, params);
        if (objects != null && objects.size() == 1) {
            return (T) objects.get(0);
        } else {
            throw new RuntimeException("查询结果为空或者返回结果过多");
        }
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        String methodName = method.getName();
                        String className = method.getDeclaringClass().getName();
                        String statementId = className + "." + methodName;

                        Type genericReturnType = method.getGenericReturnType();

                        SqlCommandType sqlCommandType = SqlCommandType.valueOf(
                                configuration.getMappedStatementMap().get(statementId)
                                .getSqlCommandType());
                        switch (sqlCommandType) {
                            case SELECT:
                                if (genericReturnType instanceof ParameterizedType) {
                                    List<Object> objects = selectList(statementId, args);
                                    return objects;
                                } else {
                                    return selectOne(statementId, args);
                                }
                            default:
                               return update(statementId, args);
                        }
                    }
                });
        return (T) proxyInstance;
    }

    @Override
    public int insert(String statementId) throws SQLException {
        return this.insert(statementId, null);
    }

    @Override
    public int insert(String statementId,  Object... params) throws SQLException {
        return this.update(statementId, params);
    }

    @Override
    public int update(String statementId) throws SQLException {
        return this.update(statementId, null);
    }

    @Override
    public int update(String statementId,  Object... params) throws SQLException {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        return simpleExecutor.doUpdate(configuration, mappedStatement, params);
    }
}
